package com.lebang.controller.sys;

import com.alibaba.fastjson.JSON;
import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.AllTaskWorthRecord;
import com.lebang.domain.TaskRouse;
import com.lebang.domain.TaskWorthRecord;
import com.lebang.entity.sys.LbbUser;
import com.lebang.example.AllTaskWorthRecordExample;
import com.lebang.mapper.LbbUserMapper;
import com.lebang.model.MyConfig;
import com.lebang.service.AllTaskWorthRecordService;
import com.lebang.service.NewRouseService;
import com.lebang.service.UserService;
import com.lebang.service.buss.MemberService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;


/**
 * 总任务价值录制
 *
 * @author xwj
 * @date 2020-09-18
 */
@Controller
@RequestMapping("/sys/alltaskworthrecord")
public class AllTaskWorthRecordController {
    @Autowired
    private AllTaskWorthRecordService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Autowired
    private LbbUserMapper lbbUserMapper;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        String userId = (String) session.getAttribute("userid");
        LbbUser lbbUser = lbbUserMapper.getByUserId(userId);
      //  rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        if(MyConfig.getEnv()==1){
            if(lbbUser.getUsername().equals("fangjf")){
                model.addAttribute("fishMark",1);
            }else{
                model.addAttribute("fishMark",0);
            }

        }else{
            model.addAttribute("fishMark",0);
        }
        return "/alltaskworthrecord/list";
    }

    @Login
    @GetMapping("/add")
    @DataSource("commonDB")
    public String add(Integer id, Model model) {
        AllTaskWorthRecord vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AllTaskWorthRecord();
        }
        vo.setAppid(MyConfig.getEnv());
        model.addAttribute("item", vo);
        List<TaskRouse> rouses = newRouseService.selectAll();
        //  rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("ite", rouses);
        return "/alltaskworthrecord/edit";
    }


    @Login
    @GetMapping("/editPage")
    @DataSource("commonDB")
    public String editPage(Integer id, Model model) {
        AllTaskWorthRecord vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AllTaskWorthRecord();
        }
        vo.setAppid(MyConfig.getEnv());
        model.addAttribute("item", vo);
        List<TaskRouse> rouses = newRouseService.selectAll();
        //  rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("ite", rouses);
        return "/alltaskworthrecord/editPage";
    }

    @Login
    @PostMapping("/edit")
    @ResponseBody

    public JsonData edit(AllTaskWorthRecord vo) {

        vo = baseService.checkData(vo);

        if (vo.getId() != null) {
            baseService.update(vo);
        } else {
            vo.setCreateTime(LocalDate.now());
            if(baseService.checkExit(vo)){
                return Callback.success("success", "当前记录日期对应的数据源任务价值已存在");
            }
            baseService.insert(vo);
        }
        return Callback.success("success", "保存成功");
    }


    @Login
    @PostMapping("/saveEdit")
    @ResponseBody
    public JsonData saveEdit(String records) {
        List<AllTaskWorthRecord> recordList = JSON.parseArray(records, AllTaskWorthRecord.class);
        recordList = baseService.calculationAllRecord(recordList);
        baseService.insertList(recordList);
        return Callback.success("success", "保存成功");
    }

    /**
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/page")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public HashMap newlist(String sdkKey, Long page, Integer limit, Integer appId,Integer star,Integer end,HttpSession session) {
        HashMap map = new HashMap(1);
        AllTaskWorthRecordExample example = new AllTaskWorthRecordExample();
        AllTaskWorthRecordExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc");
        if (!TextUtil.isEmpty(appId)) {
            c.andAppidEqualTo(appId);
        }else{
            c.andAppidEqualTo(MyConfig.getEnv());
        }
        if (!TextUtil.isEmpty(sdkKey)) {
            c.andSdkKeyEqualTo(sdkKey);
        } else {
            sdkKey = null;
        }
        if(!TextUtil.isEmpty(star)&&!TextUtil.isEmpty(end)){
            c.andTimeBetween(star,end);
        }
        if(newRouseService.isRoot(session)){
            c.andSdkKeyNotIn(sdkList);
        }
        map.put("count", baseService.countByExample(example));
        map.put("code", 0);
        List<AllTaskWorthRecord> masters = baseService.selectByExample(example);
        map.put("data", masters);
        return map;
    }


    @Login
    @PostMapping("/delete")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }


}
